package com.atguigu.eduservice.service.impl;

import com.alibaba.excel.EasyExcel;
import com.atguigu.eduservice.entity.EduSubject;
import com.atguigu.eduservice.entity.excel.ExcelSubjectData;
import com.atguigu.eduservice.entity.subject.OneSubject;
import com.atguigu.eduservice.entity.subject.TwoSubject;
import com.atguigu.eduservice.listener.SubjectReadListener;
import com.atguigu.eduservice.mapper.EduSubjectMapper;
import com.atguigu.eduservice.service.EduSubjectService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @author kabuda
 * @description 针对表【edu_subject(课程科目)】的数据库操作Service实现
 * @createDate 2023-04-24 14:04:24
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject>
        implements EduSubjectService {

    @Override
    public void saveSubject(MultipartFile file) {
        InputStream stream = null;
        try {
            stream = file.getInputStream();
            EasyExcel.read(stream, ExcelSubjectData.class, new SubjectReadListener(this)).sheet("课程列表").doRead();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void exportExcel(HttpServletResponse response) {
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("UTF-8");
        String fileName = "subject";
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");


        //一级分类
        QueryWrapper<EduSubject> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id", 0).select("title","id");
        List<EduSubject> oneLevel = this.list(wrapper);

        ArrayList<ExcelSubjectData> excelSubjectData = new ArrayList<>();
        oneLevel.stream().forEach(item -> {
            //一级分类标题
            String title = item.getTitle();

            //对应一级分类的二级分类
            String id = item.getId();
            QueryWrapper<EduSubject> wrapper1 = new QueryWrapper<>();
            wrapper1.eq("parent_id", id).select("title");
            List<EduSubject> towLevel = this.list(wrapper1);
            //封装数据
            towLevel.forEach(two->{
                ExcelSubjectData data = new ExcelSubjectData();
                data.setOneSubjectName(title);
                data.setTwoSubjectName(two.getTitle());
                excelSubjectData.add(data);
            });

        });
        try {
            EasyExcel.write(response.getOutputStream(), ExcelSubjectData.class).sheet("课程列表").doWrite(excelSubjectData);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    @Override
    public List<OneSubject> getNestedTreeList() {
        //查询一级分类
        QueryWrapper<EduSubject> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("parent_id",0);
        List<EduSubject> one = this.list(wrapper1);
        //封装一级分类
        List<OneSubject> oneSubjects=new ArrayList<>();
        for (EduSubject eduSubject : one) {
            OneSubject oneSubject = new OneSubject();
            BeanUtils.copyProperties(eduSubject,oneSubject);
            oneSubjects.add(oneSubject);
        }
        //查询二级分类
        QueryWrapper<EduSubject> wrapper2 = new QueryWrapper<>();
        wrapper1.ne("parent_id",0);
        List<EduSubject> two = this.list(wrapper2);

        //填充二级分类封装且填充到对应一级分类
        for (OneSubject oneSubject : oneSubjects) {
            List<TwoSubject> twoSubjects=new ArrayList<>();
            for (EduSubject eduSubject : two) {
                if(eduSubject.getParentId().equals(oneSubject.getId())){
                    TwoSubject twoSubject = new TwoSubject();
                    BeanUtils.copyProperties(eduSubject,twoSubject);
                    twoSubjects.add(twoSubject);
                }
            }
            oneSubject.setChildren(twoSubjects);
        }
        return oneSubjects;
    }
}




